
namespace CSharpDuino
{
    using System.Threading;
    using Microsoft.SPOT.Hardware;
                
    public class Blinker
    {
        private readonly OutputPort ledPin;

        public int Delay { get; set; }
    
        private readonly Thread t;

        public bool State { get; set; }

        public Blinker(Cpu.Pin p, int delay)
        {
            this.ledPin = new OutputPort(p, false);
            this.Delay = delay;
            this.t =  new Thread(this.Loop);
            t.Start();
        }

        private void Loop()
        {
            while (true)
            {
                if (this.State)
                {
                    ledPin.Write(!ledPin.Read());
                    Thread.Sleep(this.Delay);
                }
                else
                {
                    ledPin.Write(false);
                }
            }
        }
    }
}
